<?php

class LoginForm extends sfForm
{
  public function configure()
  {
     $this->setWidgets(array(
      'username'  => new sfWidgetFormInput(),
      'password'  => new sfWidgetFormInputPassword(),
      
    ));

    $this->setValidators(array(
      'username' => new sfValidatorString(array('required' => true)),
      'password' => new sfValidatorString(array('required' => true)),
    ));

    $this->widgetSchema->setNameFormat('login[%s]');

      

    $this->validatorSchema->setPostValidator(
        new sfValidatorCallback(array('callback' => array($this, 'checkPassword')))
    );



  }
  public function checkPassword($validator, $values)
   {
      
      $c=new Criteria();
      $c->add(KupacPeer::KORISNICKO_IME,$values['username'], Criteria::EQUAL);
      $kupac = KupacPeer::doSelectOne($c);
      if ($kupac==null) {
            $error = new sfValidatorError($validator, 'Ne valja ime');
            throw new sfValidatorErrorSchema($validator, array('username' => $error));
      }

      $c=new Criteria();
      $c->add(KupacPeer::KORISNICKO_IME,$values['username'], Criteria::EQUAL);
      $c->add(KupacPeer::KORISNICKO_IME,$values['password'], Criteria::EQUAL);
      $kupac = KupacPeer::doSelectOne($c);
      if ($kupac==null) {
            $error = new sfValidatorError($validator, 'Ne valja sifra');
            throw new sfValidatorErrorSchema($validator, array('password' => $error));
      }
        
        return $values;
}
}
